<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class c_main_class extends AWS_CONTROLLER
{

	public function get_access_rule()
	{
		$rule_action['rule_type'] = "black"; //黑名单,黑名单中的检查  'white'白名单,白名单以外的检查
		$rule_action['guest'] = array();
		$rule_action['user'] = array();
		
		return $rule_action;
	}

	public function setup()
	{
		$this->per_page = get_setting('contents_per_page');
		
		$this->crumb('话题', '/topic/');
	}

	public function hot_action()
	{
		$this->per_page = 12;
		$min_scores = 1;
		$content_category = $this->model('module')->content_category();
		
		if ($_GET['category'] and $category_info = $this->model('system')->get_category_info($_GET['category']))
		{
			TPL::assign('category_info', $category_info);
		}
		
		if ($_GET['category'])
		{
			// 			if ($topics = $this->model('topic')->get_hot_topic($_GET['category'], 0, calc_page_limit($_GET['page'], $this->per_page)))
			// 			{
			// 				foreach ($topics['topics'] as $key => $val)
			// 				{
			

			// 					$cache_key = 'topic_get_best_answer_list_uid_' . $val['topic_id'] . '_' . $this->user_id;
			

			// 					$best_answer_uid = $this->model('db_cache')->load($cache_key);
			

			// 					if (! $best_answer_uid)
			// 					{
			// 						if ($best_answer = $this->model('topic')->get_best_answer_list($val['topic_id'], $this->user_id))
			// 						{
			// 							foreach ($best_answer as $key2 => $val2)
			// 							{
			// 								$best_answer_uid[] = $val2['uid'];
			// 							}
			// 							$this->model('db_cache')->save($cache_key, $best_answer_uid, 60 * 60 * 24);
			// 						}
			// 					}
			

			// 					if ($best_answer_uid)
			// 					{
			// 						$best_answer_uid = array_slice($best_answer_uid, 0, 4);
			

			// 						foreach ($best_answer_uid as $key3 => $val3)
			// 						{
			

			// 							$topics['topics'][$key]['best_answer'][] = $this->model('account')->get_users_by_uid($val3);
			// 						}
			

			// 					}
			

			// 				}
			

			// 				$this->model('pagination')->initialize(array(
			// 						'base_url' => get_js_url('/topic/hot/category-' . $_GET['category']),
			// 						'total_rows' => $topics['topics_count'],
			// 						'per_page' => $this->per_page
			// 				));
			// 			}
			

			// 			TPL::assign('topics', $topics['topics']);
			// 			TPL::assign('pagination', $this->model('pagination')->create_links());
		

		}
		else
		{
			if ($content_category)
			{
				foreach ($content_category as $key => $val)
				{
					$topics = null;
					
					$cache_key = 'topic_get_hot_topic_' . $val['id'] . '_0_8';
					
					$topics = $this->model('db_cache')->load($cache_key);
					
					if (! is_array($topics))
					{
						$topics = $this->model('topic')->get_hot_topic($val['id'], 0, 8);
						$this->model('db_cache')->save($cache_key, $topics, 60 * 60 * 1);
					}
					
					$content_category[$key]['topics'] = $topics['topics'];
					
					if ($content_category[$key]['topics'])
					{
						
						foreach ($content_category[$key]['topics'] as $tkey => $tval)
						{
							
							if ($tval['scores'] < $min_scores)
							{
								unset($content_category[$key]['topics'][$tkey]);
								continue;
							
							}
							
							$content_category[$key]['topics'][$tkey]['has_focus'] = $this->model('topic')->has_focus_topic($this->user_id, $tval['topic_id']);
							$cache_key = 'topic_get_best_answer_list_uid_' . $tval['topic_id'] . '_' . $this->user_id;
							
							$best_answer_uid = $this->model('db_cache')->load($cache_key);
							
							if (! $best_answer_uid)
							{
								if (is_array($best_answer_uid))
								{
									continue;
								}
								
								if ($best_answer = $this->model('topic')->get_best_answer_list($tval['topic_id'], $this->user_id))
								{
									foreach ($best_answer as $key2 => $val2)
									{
										$best_answer_uid[] = $val2['uid'];
									}
									if (! $best_answer_uid)
									{
										$best_answer_uid = array();
									}
								}
								else
								{
									$best_answer_uid = array();
								}
								
								$this->model('db_cache')->save($cache_key, $best_answer_uid, 60 * 60 * 24);
							}
							
							if ($best_answer_uid)
							{
								$best_answer_uid = array_slice($best_answer_uid, 0, 4);
								
								foreach ($best_answer_uid as $key3 => $val3)
								{
									
									$content_category[$key]['topics'][$tkey]['best_answer'][] = $this->model('account')->get_users_by_uid($val3);
								}
							
							}
						
						}
					
					}
				
				}
			}
		}
		
		TPL::assign('content_category', $content_category);
		
		$this->crumb('话题广场 - 热门话题', '/topic/hot/');
		
		TPL::import_css(array(
			'css/discussion.css'
		));
		
		TPL::output('topic/square_hot');
	}

	public function new_action()
	{
		$content_category = $this->model('module')->content_category();
		
		TPL::assign('content_category', $content_category);
		
		if ($_GET['category'] and $category_info = $this->model('system')->get_category_info($_GET['category']))
		{
			TPL::assign('category_info', $category_info);
		
		}
		
		if ($_GET['category'])
		{
			$topics_count = $this->model('topic')->get_category_new_topic($_GET['category'], 'add_time>' . (time() - (60 * 60 * 24 * 30)), calc_page_limit($_GET['page'], 12), true);
			$topics = $this->model('topic')->get_category_new_topic($_GET['category'], 'add_time>' . (time() - (60 * 60 * 24 * 30)), calc_page_limit($_GET['page'], 12), false);
		}
		else
		{
			$topics_count = $this->model('topic')->get_topic_list('add_time > ' . (time() - (60 * 60 * 24 * 30)), null, true);
			$topics = $this->model('topic')->get_topic_list('add_time>' . (time() - (60 * 60 * 24 * 30)), calc_page_limit($_GET['page'], 12));
		}
		
		$this->model('pagination')->initialize(array(
			'base_url' => get_js_url('/topic/new/category-' . $_GET['category']), 
			'total_rows' => $topics_count, 
			'per_page' => $this->per_page
		));
		
		TPL::assign('topics', $topics);
		TPL::assign('pagination', $this->model('pagination')->create_links());
		
		$this->crumb('话题广场 - 最新话题', '/topic/new/');
		
		TPL::import_css(array(
			'css/discussion.css'
		));
		
		TPL::output('topic/square');
	}

	public function category_action()
	{
		if ($content_category = $this->model('module')->content_category())
		{
			foreach ($content_category as $key => $val)
			{
				$content_category[$key]['topics'] = $this->model('topic')->get_category_topic($val['id'], 0, null);
			}
		}
		
		TPL::assign('content_category', $content_category);
		
		$this->crumb('话题广场 - 分类话题', '/topic/category/');
		
		TPL::import_css(array(
			'css/discussion.css'
		));
		
		TPL::output('topic/square');
	}

	public function index_action()
	{
		if ($_GET['id'] or $_GET['title'])
		{
			$this->_topic();
		}
		else
		{
			$this->hot_action();
		}
	}

	public function _topic()
	{
		$uid = $this->user_id;
		$topic_id = $_GET['id'];
		$topic_title = trim($_GET['title']);
		
		if ($topic_title)
		{
			$topic_id = $this->model('topic')->get_topic_id_by_title($topic_title, get_setting('cache_level_high')); //60秒缓存
			

			if ($topic_id > 0)
			{
				$topic_info = $this->data_cache(ZCACHE::format_key('topic_get_topic_by_title_' . base64_encode($topic_title)), '$this->model("topic")->get_topic_by_title("' . $topic_title . '")', get_setting('cache_level_high'), ZCACHE::format_key("topic_info_" . $topic_id));
			}
		}
		else if ($topic_id)
		{
			$topic_info = $this->model('topic')->get_topic_by_title($topic_id);
			
			if (! $topic_info) //避免有时 topic title 是全数字
			{
				$topic_info = $this->model('topic')->get_topic($topic_id);
			}
		}
		
		if (empty($topic_info))
		{
			H::redirect_msg('话题不存在', '/');
		}
		
		$topic_id = $topic_info['topic_id'];
		
		$best_answer = $this->model('topic')->get_best_answer_list($topic_info['topic_id'], $this->user_id);
		
		if ($best_answer)
		{
			$best_answer = array_slice($best_answer, 0, 5);
			
			TPL::assign('best_answer', $best_answer);
		}
		//p($best_answer);
		//$no_answer_question_count = $this->model('question')->get_question_info($topic_info['topic_id'], "", 0, true, null, null, 0);
		

		$no_answer_question_count = $this->model('question')->get_question_count_from_topic($topic_info['topic_id'], 0);
		
		if (empty($topic_info['topic_pic']))
		{
			$topic_info['topic_pic_max'] = G_STATIC_URL . '/common/topic-max-img.jpg';
		}
		else
		{
			$topic_info['topic_pic_max'] = get_setting('upload_url') . '/topic/' . str_replace(AWS_APP::config()->get('image')->topic_thumbnail['min']['w'] . '_' . AWS_APP::config()->get('image')->topic_thumbnail['min']['h'], AWS_APP::config()->get('image')->topic_thumbnail['max']['w'] . '_' . AWS_APP::config()->get('image')->topic_thumbnail['max']['h'], $topic_info['topic_pic']);
		}
		
		$topic_info['has_focus'] = $this->model('topic')->has_focus_topic($this->user_id, $topic_info['topic_id']);
		
		$topic_info['topic_experience'] = $this->model('topic')->get_topic_experience($uid, $topic_id);
		
		$best_question_count = $this->model('topic')->get_all_best_question_by_topic($topic_info['topic_id'], "", true);
		
		TPL::assign('topic_info', $topic_info);
		TPL::assign('no_answer_question_count', $no_answer_question_count);
		TPL::assign('best_question_count', $best_question_count);
		
		$this->crumb($topic_info['topic_title'], '/topic/' . $topic_info['topic_title']);
		
		if ($topic_info['topic_description'])
		{
			TPL::set_meta('description', $topic_info['topic_title'] . ' - ' . cjk_substr(str_replace("\r\n", ' ', strip_tags($topic_info['topic_description'])), 0, 128, 'UTF-8', '...'));
		}
		
		TPL::assign('related_topics', $this->model('topic')->related_topics($topic_id));
		
		TPL::import_css(array(
			'css/discussion.css'
		));
		
		TPL::import_js(array(
			'js/ajaxupload.js'
		));
		
		TPL::output('topic/index');
	}

	public function detail_action()
	{
		if (! $topic_info = $this->model('topic')->get_topic($_GET['id']))
		{
			H::redirect_msg('话题不存在', '/');
		}
		
		if (! $this->user_id)
		{
			if ($_GET['fromuid'])
			{
				HTTP::set_cookie('fromuid', $_GET['fromuid']);
			}
			
			if ($_GET['fromemail'])
			{
				HTTP::set_cookie('fromemail', $_GET['fromemail']);
			}
		}
		
		$topic_info['topic_description'] = FORMAT::parse_links($topic_info['topic_description']);
		
		$log_list = ACTION_LOG::get_action_by_event_id(false, $_GET['id'], 10, ACTION_LOG::CATEGORY_TOPIC, '401, 402, 403, 404, 405, 410, 411');
		$log_list = $this->model('topic')->analysis_log($log_list);
		
		$this->crumb('话题详情', '/topic/' . $topic_info['topic_title']);
		$this->crumb($topic_info['topic_title'], '/topic/detail/' . $topic_info['topic_id']);
		
		TPL::assign('topic_info', $topic_info);
		TPL::assign('related_topics', $this->model('topic')->related_topics($_GET['id']));
		TPL::assign('log_list', $log_list);
		
		if ($topic_info['topic_description'])
		{
			TPL::set_meta('description', $topic_info['topic_title'] . ' - ' . cjk_substr(str_replace("\r\n", ' ', strip_tags($topic_info['topic_description'])), 0, 128, 'UTF-8', '...'));
		}
		
		TPL::import_css(array(
			'css/discussion.css'
		));
		
		TPL::import_js(array(
			'js/ajaxupload.js'
		));
		
		TPL::output('topic/detail');
	}
}